package my.test.hencodertest;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import my.test.hencodertest.view.PaperFolding;

public class PaperFoldingActivity extends AppCompatActivity {
    PaperFolding paperFolding;
    AnimatorSet animatorSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paper_folding);
        paperFolding = findViewById(R.id.paperFolding);


        ValueAnimator degreeAnimator = ValueAnimator.ofInt(0, 360);
        degreeAnimator.setDuration(2000);
        degreeAnimator.setInterpolator(new AccelerateInterpolator());
        degreeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                paperFolding.setDegree((int) animation.getAnimatedValue());
            }
        });

        ValueAnimator foldingSizeAnimator = ValueAnimator.ofFloat(0, 200);
        foldingSizeAnimator.setDuration(500);
        foldingSizeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                paperFolding.setFoldingSize((float) animation.getAnimatedValue());
            }
        });

        ValueAnimator foldingEndSizeAnimator = ValueAnimator.ofFloat(200, 0);
        foldingEndSizeAnimator.setDuration(500);
        foldingEndSizeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                paperFolding.setFoldingSize((float) animation.getAnimatedValue());
            }
        });

        animatorSet = new AnimatorSet();
        animatorSet.playSequentially(foldingSizeAnimator, degreeAnimator, foldingEndSizeAnimator);

        paperFolding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (animatorSet.isRunning()) {
                    animatorSet.cancel();
                }
                animatorSet.start();
            }
        });

    }
}
